package com.deyuanyun.pic.insp.controller.inspresult;

import com.deyuanyun.pic.common.util.RequestUtil;
import com.deyuanyun.pic.common.util.ResponseUtil;
import com.deyuanyun.pic.insp.util.InspExportUtil;
import com.deyuanyun.pic.settings.controller.AbstractBaseController;
import com.deyuanyun.pic.settings.service.base.CodeService;
import com.deyuanyun.pic.insp.service.inspresult.MetalLossService;
import com.deyuanyun.pic.insp.service.inspresult.PatchService;
import com.deyuanyun.pic.settings.support.utils.ExcelUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;


/**
 * 
 * 内检测  检测数据   金属外接物/修补补丁
 *
 * @author    scalpel.Wang
 * @version   2015年10月8日
 * @see         // 引用的类
 * Method List:                       
 *         1.toPage   ---------------- 去主页
 *         2.search   ---------------- 搜索数据
 *         3.exportExl---------------- 导出数据
 */
@Controller
@RequestMapping("/insp/patch")
public class PatchController extends AbstractBaseController {
	@Resource
	MetalLossService metalLossService;
	
	@Resource
	PatchService patchService;
	
	@Resource
	CodeService codeService;

	@RequestMapping("/page.html")
	public ModelAndView toPage(HttpServletRequest request,String lineId,String taskSegmentId){
		ModelAndView mav=new ModelAndView();
		
		//查询字典数据 金属损伤类型
		List<HashMap> codeList= codeService.selectList("DENT_TYP");
		
		//查询管道名称 
		List lineList=lineService.getLine();

		
		HashMap map= RequestUtil.parseRequestParamToHashMap(request);
		mav.addAllObjects(map);
		mav.addObject("codeList", codeList);
		mav.addObject("lineList", lineList);
		mav.setViewName("/insp/inspresult/patch");
		return mav;
		
	}
	/**
	 * 
	  * 查询金属外接物/修补补丁数据
	  *
	  * @param current 当前页 pageCount每页显示数量
	  * @return    // 方法返回值的说明
	  *
	  *
	 */
	@RequestMapping("/search.json")
	public void search(@RequestParam(required = false) Integer current,
					   @RequestParam(required = false) Integer pageCount,
					   HttpServletRequest request,
			           HttpServletResponse response){
		
		int pageNo=1;
		int pageSize=10;
		if(null != current)
		{
			pageNo=current;
		}
		
		if(null != pageCount)
		{
			pageSize=pageCount;
		}
		PageHelper.startPage(pageNo, pageSize);
		HashMap map= RequestUtil.parseRequestParamToHashMap(request);
		Page main=patchService.findPage(map);
		ResponseUtil.printl(response, ResponseUtil.getJson(main).toString(), "JSON");
	}
	/**
	 * 
	  * 导出金属外接物/修补补丁数据
	  *
	  * @return    // 方法返回值的说明
	  *
	  *
	 */
	@RequestMapping("/exportExl")
	public void exportExl(HttpServletRequest request,HttpServletResponse response){
		HashMap param= RequestUtil.parseRequestParamToHashMap(request);
		List result=patchService.findObjectList(param);
		param.put("data",result);
		try {
			InspExportUtil.exportExcel(request.getRealPath("/")+"/WEB-INF/classes/template/excl/insp/patch.xlsx",param,response,"补丁");
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	@RequestMapping("/exportApprarse")
	public void exportApprarse(HttpServletRequest request,HttpServletResponse response){

		HashMap param= RequestUtil.parseRequestParamToHashMap(request);
		InputStream filePath =this.getClass().getResourceAsStream("/template/excl/insp/apprarse.xls");
		int[] lineStart = new int[] { 4 };
		List<List<String[]>> result=patchService.queryExportApprarse(param);
		InspExportUtil.exportApprarse("修补补丁", result, response,request);
	}
}
